package com.rolfhoward.video.record.widget

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout

/**
 *@author wuqiubin
 *@date 2023/1/15
 *@Describe 自动填充适配布局，并带动画效果
 */
class AutoFillTranslateView : FrameLayout{
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var isInit = true
    fun setTranslate(height: Int){
        val animator: ValueAnimator = if (this.height > height
            || height == 0 && this.height > 0
            || height > 0 && this.height == 0
            || this.height != 0 && this.height < height
        ) {
            ValueAnimator.ofInt(this.height, height)
        } else {
            ValueAnimator.ofInt(height, this.height)
        }
        animator.addUpdateListener {
            val animatedValue = it.animatedValue as Int
            this.layoutParams?.height = animatedValue
            requestLayout()
        }
        if (isInit){
            isInit = false
            animator.duration = 0
        }else{
            animator.duration = 200
        }
        animator.start()
    }
}